<?php

App::uses('AppController', 'Controller');

class CalendarController extends AppController
{
    public $name = 'Calendar';
    public $uses = array('Event');
    public $helpers = array('Tinymce');
     
    
    function beforeFilter() {
       
    }
    
    // Páginas do CMS AQUI
    function admin_index()
    {
        $this->layout = 'admin';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);
    }
    
    function admin_calendar()
    {
        $this->layout = 'calendar';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);
    }
    
    function admin_events()
    {
        $this->layout = 'ajax';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);
    }
    
    function admin_management()
    {
        $this->layout = 'admin';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);
    }

    function admin_add()
    {
        $this->layout = 'admin';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);
        
        if($this->request->is('post'))
        {
//            die(debug($this->data));
            if ($this->Event->save($this->request->data))
            {
                // Set a session flash message and redirect.
                $this->Session->setFlash('Criado com sucesso.', 'default', array(), 'good');
                $this->redirect('/admin/pages');
            }
        }
    }
    
    function admin_edit($id)
    {
        $this->layout = 'admin';
        
        $Events = $this->Event->find('all');
        $this->set('Events', $Events);

        if($this->data)
        {
            //Alterações antes de salvar
            $this->request->data['Event']['date'] = $this->dataPortugues($this->data['Event']['date']);
            if(!isset($this->request->data['Event']['type'])){$this->request->data['Event']['type'] = 0;}
            
            //Salvar dado
            if ($this->Event->save($this->request->data))
            {
                // Set a session flash message and redirect.
                $this->Session->setFlash('Atualizado com sucesso.', 'default', array(), 'good');
                $this->redirect('/admin/calendar/edit/'.$id);
            }
         }
         else
         {
            $this->data = $this->Event->read(null,$id);
            $this->request->data['Event']['date'] = $this->dataPortugues($this->data['Event']['date']);
         }
        
        
    }
    
    function admin_delete($id)
    {
        $this->Event->delete($id);
        $this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
        $this->redirect('/admin/pages');
    }

    
    // Páginas do SITE AQUI
    function index()
    {
        
    }
}
